package com.zh.work.controller.platform.trans;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zh.common.entity.JSONResult;
import com.zh.common.util.DateSetUtil;
import com.zh.work.dao.platform.trans.NotifyMerchTransMapper;
import com.zh.work.entity.trans.NotifyMerchTransEntity;
import com.zh.work.service.platform.trans.NotifyMerchTransService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * code is far away from bug with the animal protecting
 * ┏┓　　　┏┓
 * ┏┛┻━━━┛┻┓
 * ┃　　　　　　　┃
 * ┃　　　━　　　┃
 * ┃　┳┛　┗┳　┃
 * ┃　　　　　　　┃
 * ┃　　　┻　　　┃
 * ┃　　　　　　　┃
 * ┗━┓　　　┏━┛
 * 　　┃　　　┃神兽保佑
 * 　　┃　　　┃代码无BUG！
 * 　　┃　　　┗━━━┓
 * 　　┃　　　　　　　┣┓
 * 　　┃　　　　　　　┏┛
 * 　　┗┓┓┏━┳┓┏┛
 * 　　　┃┫┫　┃┫┫
 * 　　　┗┻┛　┗┻┛
 *
 * @Description: 交易消息管理
 * @Author: Li.HuiNeng
 * @Create in: 2019/09/12
 * @Version: 1.0.1
 */
@Api(value = "交易消息管理控制器", produces = "application/json", tags = {"交易消息管理控制器"})
@RestController
@RequestMapping("/notifyMerchTrans")
public class NotifyMerchTransController {

    /**
     * @Description: 简易crud时使用
     * ------------------------
     * @Author: Li.HuiNeng
     * @Date: 2019/09/12
     */
    @Autowired
    private NotifyMerchTransMapper mapper;

    /**
     * @Description: 有业务逻辑需要处理时使用
     * ------------------------
     * @Author: Li.HuiNeng
     * @Date: 2019/09/12
     */
    @Autowired
    private NotifyMerchTransService service;


/*
    @ApiOperation(value = "新增一条记录", tags = {"putNotifyMerchTrans"}, notes = "新增一条记录")
    @PutMapping("/putNotifyMerchTrans")
    @ResponseBody
    public JSONResult<Object> putNotifyMerchTrans(@ApiParam(value = "交易消息实体类", name = "notifyMerchTransEntity")
                                                  @RequestBody NotifyMerchTransEntity notifyMerchTransEntity) {
        JSONResult<Object> jsonResult = new JSONResult<>();
        try {
            if (notifyMerchTransEntity.insert()) {
                jsonResult.setCode(200);
                jsonResult.setMessage("添加完成");
                jsonResult.setSuccess(true);
            } else {
                jsonResult.setMessage("添加失败,请检查参数是否填写完整!");
                jsonResult.setSuccess(false);
            }
        } catch (Exception e) {
            jsonResult.setCode(e.hashCode());
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
            e.printStackTrace();
        }
        return jsonResult;
    }


    @ApiOperation(value = "通过id修改信息", tags = {"updateNotifyMerchTransById"}, notes = "通过id修改信息")
    @PutMapping("/updateNotifyMerchTransById")
    @ResponseBody
    public JSONResult<Object> updateNotifyMerchTransById(@ApiParam(value = "交易消息实体类", name = "notifyMerchTransEntity")
                                                         @RequestBody NotifyMerchTransEntity notifyMerchTransEntity) {
        JSONResult<Object> jsonResult = new JSONResult<>();
        try {
            if (notifyMerchTransEntity.updateById()) {
                jsonResult.setCode(200);
                jsonResult.setMessage("修改完成");
                jsonResult.setSuccess(true);
            } else {
                jsonResult.setMessage("修改失败,请检查参数是否填写完整!");
                jsonResult.setSuccess(false);
            }
        } catch (Exception e) {
            jsonResult.setCode(e.hashCode());
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setSuccess(false);
            e.printStackTrace();
        }
        return jsonResult;
    }

    @ApiOperation(value = "根据主键删除记录", tags = {"deleteNotifyMerchTransById"}, notes = "根据主键删除记录")
    @DeleteMapping("/deleteNotifyMerchTransById")
    public JSONResult<Object> deleteNotifyMerchTransById(@ApiParam(value = "交易消息实体类", name = "notifyMerchTransEntity") NotifyMerchTransEntity notifyMerchTransEntity) {
        JSONResult<Object> jsonResult = new JSONResult<>();
        try {
            if (notifyMerchTransEntity.deleteById()) {
                jsonResult.setCode(200);
                jsonResult.setMessage("删除完成");
                jsonResult.setSuccess(true);
            } else {
                jsonResult.setMessage("删除失败,请检查参数是否填写完整!");
                jsonResult.setSuccess(false);
            }
        } catch (Exception e) {
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setCode(e.hashCode());
            e.printStackTrace();
        }
        return jsonResult;
    }

    @ApiOperation(value = "根据主键列表批量删除记录", tags = {"deleteNotifyMerchTransBatchByIds"}, notes = "该传参时 请选择raw(application/json)格式传入")
    @DeleteMapping("/deleteNotifyMerchTransBatchByIds")
    @ResponseBody
    public JSONResult<Object> deleteNotifyMerchTransBatchByIds(@ApiParam(value = "主键集合", name = "ids") @RequestBody List<Integer> ids) {
        JSONResult<Object> jsonResult = new JSONResult<>();
        try {
            int c = this.mapper.deleteBatchIds(ids);
            if (c > 0) {
                jsonResult.setCode(200);
                jsonResult.setMessage("删除完成");
                jsonResult.setSuccess(true);
            } else {
                jsonResult.setMessage("删除失败,请检查参数是否填写完整!");
                jsonResult.setSuccess(false);
            }
        } catch (Exception e) {
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setCode(e.hashCode());
            e.printStackTrace();
        }
        return jsonResult;
    }

    @ApiOperation(value = "根据id查询详细信息", tags = {"getNotifyMerchTransById"}, notes = "根据id查询详细信息")
    @GetMapping("/getNotifyMerchTransById")
    public JSONResult<NotifyMerchTransEntity> getNotifyMerchTransById(@ApiParam(value = "交易消息实体类", name = "notifyMerchTransEntity")
                                                                              NotifyMerchTransEntity notifyMerchTransEntity) {
        JSONResult<NotifyMerchTransEntity> jsonResult = new JSONResult<>();
        try {
            NotifyMerchTransEntity result = (NotifyMerchTransEntity) notifyMerchTransEntity.selectById(notifyMerchTransEntity.getId());
            if (result != null) {
                jsonResult.setCode(200);
                jsonResult.setMessage("查询完成");
                jsonResult.setSuccess(true);
                jsonResult.setData(result);
            } else {
                jsonResult.setMessage("未查询到相应信息，请检查参数是否正确");
                jsonResult.setSuccess(false);
            }
        } catch (Exception e) {
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setCode(e.hashCode());
            e.printStackTrace();
        }
        return jsonResult;
    }*/


    @ApiOperation(value = "分页查询参数数据集", tags = {"getPagesNotifyMerchTrans"}, notes = "分页查询参数数据集")
    @GetMapping("/getPagesNotifyMerchTrans")
    public JSONResult<List<NotifyMerchTransEntity>> getPagesNotifyMerchTrans(
            @ApiParam(value = "起始页", name = "pageNo") Integer pageNo,
            @ApiParam(value = "当前页显示数量", name = "pageSize") Integer pageSize,
            @ApiParam(value = "交易消息实体类", name = "notifyMerchTransEntity") NotifyMerchTransEntity notifyMerchTransEntity) {
        JSONResult<List<NotifyMerchTransEntity>> jsonResult = new JSONResult<>();
        try {
            IPage<NotifyMerchTransEntity> result = this.service.selectNotifyMerchTransPages(new Page<>(pageNo, pageSize), notifyMerchTransEntity);
            jsonResult.setTotal(result.getTotal());
            jsonResult.setData(result.getRecords());
            jsonResult.setMessage("操作成功");
            jsonResult.setSuccess(true);
            jsonResult.setCode(200);
        } catch (Exception e) {
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setSuccess(false);
            e.printStackTrace();
        }
        return jsonResult;
    }

    @ApiOperation(value = "查询商户交易通知统计数据", tags = {"getNotifyMerchTransStatistic"}, notes = "查询商户交易通知统计数据")
    @GetMapping("/getNotifyMerchTransStatistic")
    public JSONResult<List<Map<String, String>>> getNotifyMerchTransStatistic(@ApiParam(value = "商户交易通知实体信息", name = "notifyMerchTransEntity")
                                                                       NotifyMerchTransEntity notifyMerchTransEntity) {
        JSONResult<List<Map<String, String>>> jsonResult = new JSONResult<>();
        List<Map<String, String>> list = null;
        if (Objects.isNull(notifyMerchTransEntity.getAddStartTime())) {
            notifyMerchTransEntity.setAddStartTime(DateSetUtil.getDayFirstSec(new Date()));
        }
        if (Objects.isNull(notifyMerchTransEntity.getAddEndTime())) {
            notifyMerchTransEntity.setAddEndTime(DateSetUtil.getDayLastSec(new Date()));
        }
        try {
            list = this.service.getNotifyMerchTransStatistic(notifyMerchTransEntity);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setSuccess(false);
        }
        if (list != null) {
            if (list.size() > 0) {
                jsonResult.setSuccess(true);
                jsonResult.setData(list);
            } else {
                jsonResult.setSuccess(false);
                jsonResult.setMessage("统计数量为0");
            }
        } else {
            jsonResult.setSuccess(false);
            jsonResult.setMessage("统计数量为空");
        }
        return jsonResult;
    }
}
